//
//  HNSH_ChatReceiveRedViewController.m
//  NongYe
//
//  Created by 李宇廷 on 2023/2/6.
//

#import "HNSH_ChatReceiveRedViewController.h"
#import "HNSH_ChatReceiveRedCell.h"
#import "HNSH_ChatReceiveRedHeader.h"
#import "HNSH_ChatReceiveRedChangeAlert.h"

@interface HNSH_ChatReceiveRedViewController ()

/* dataArray */
@property (nonatomic, strong) NSMutableArray *dataArray;

/* rightItemButton */
@property (nonatomic, strong) UIButton *rightItemButton;

/* headerView */
@property (nonatomic, strong) HNSH_ChatReceiveRedHeader *headerView;

/* currentIndex 0 : 收到的红包  1 : 发出的红包 */
@property (nonatomic, assign) NSInteger currentIndex;

/* redInfoModel */
@property (nonatomic, strong) NYChatRedInfoModel *redInfoModel;

@end

static NSString *HNSH_ChatReceiveRedCellID = @"HNSH_ChatReceiveRedCellID";

@implementation HNSH_ChatReceiveRedViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 128)];
    imgV.image = [UIImage imageNamed:@"chat_open_red_detail_bg"];
    [self.view addSubview:imgV];
    [self.view sendSubviewToBack:imgV];
    
    self.currentIndex = 0;
    [self showNavTitle:@"红包详情" showLeftItem:YES];
    self.navView.backgroundColor = [UIColor clearColor];
    self.navView.titleLabel.textColor = [UIColor appWhiteLabelColor];
    [self.navView.leftButton setImage:[UIImage imageNamed:@"base_nav_back_white"] forState:UIControlStateNormal];
    [self.view addSubview:self.rightItemButton];
    
    
    
//    self.page = 1;
//    [self loadNewData];
//    
    [self configTableView];
}

- (void)configTableView {
    [self setupRefresh:self.tableView option:ST_RefreshDefault];
    [self setupEmpty:self.tableView image:[UIImage imageNamed:@"empty_list_data"] title:@""];
    
    self.tableView.rowHeight = 64;
    [self.tableView registerClass:[HNSH_ChatReceiveRedCell class] forCellReuseIdentifier:HNSH_ChatReceiveRedCellID];
    self.tableView.tableHeaderView = self.headerView;
}

#pragma mark network
- (void)refreshData:(NSInteger)page {
    [HNSH_ChatNetServer requestRongCloudRedPacketRecordWithPage:page selectIndex:self.currentIndex success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            if (page == 1) {
                [self.dataArray removeAllObjects];
            }
            NSArray * records = responseObject.resultset[@"list"];
            NSArray * array = [NSArray yy_modelArrayWithClass:HNSH_ChatRedRecordModel.class json:records];
            self.redInfoModel = [NYChatRedInfoModel yy_modelWithDictionary:responseObject.resultset[@"statistics"]];
            self.headerView.model = self.redInfoModel;
            [self.dataArray addObjectsFromArray:array];
            [self endRefresh:array.count>=10];
            [self.tableView reloadData];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];

}

#pragma mark - delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HNSH_ChatReceiveRedCell *cell = [tableView dequeueReusableCellWithIdentifier:HNSH_ChatReceiveRedCellID];
    cell.model = self.dataArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    NYLSHelpCenterModel *model = self.dataArray[indexPath.row];
//    NSString *title = model.name;
//    NSString *content = model.answer;
//    CGFloat height = [content heightForFont:[UIFont systemFontOfSize:14] width:kScreenWidth-72];
//    NYLSHelpCenterAlert *alert = [[NYLSHelpCenterAlert alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 128+height+kSafeAreaBottomHeight)];
//    alert.titleString = title;
//    alert.contentString = content;
//    [alert showView];
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataArray;
}

- (UIButton *)rightItemButton {
    if (!_rightItemButton) {
        _rightItemButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightItemButton.frame = CGRectMake(kScreenWidth-48, kStatusBarHeight, 44, 44);
        [_rightItemButton setImage:[UIImage imageNamed:@"chat_sned_icon_more"] forState:UIControlStateNormal];
        __weak __typeof(&*self)weakSelf = self;
        [[_rightItemButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            HNSH_ChatReceiveRedChangeAlert *alert = [[HNSH_ChatReceiveRedChangeAlert alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 52*3+kBottomSafeHeight)];
            [alert showView];
            alert.typeBlock = ^(NSInteger index) {
                weakSelf.currentIndex = index;
                [weakSelf headerRefreshing];
                if (index == 0) {//收到的红包
                    self.navView.titleLabel.text = @"收到的红包";
                } else if (index == 1) {//发出的红包
                    self.navView.titleLabel.text = @"发出的红包";
                    
                }
            };
        }];
    }
    return _rightItemButton;
}

- (HNSH_ChatReceiveRedHeader *)headerView {
    if (!_headerView) {
        _headerView = [[HNSH_ChatReceiveRedHeader alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 244)];
        
    }
    return _headerView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
